import { View, Text } from '@tarojs/components'
import http from '../../http/http';
import { Image } from '@tarojs/components'
import { AtForm, AtInput, AtButton,AtMessage} from 'taro-ui'
import Taro from '@tarojs/taro'
import './login.less'
import { useState } from 'react'
import "taro-ui/dist/style/components/message.scss";

function Login () {
  //登录
  const [username,setUserName]=useState('')
  const [pass,setPass]=useState('')
  const home = (type) => {
    http({
      url: '/panchen/login',
      method: 'get',
      data:{
        user_name:username,
        password:pass
      }
    }).then(res => {
      if(res.message=="登录成功"){
        Taro.switchTab({
        url: '/pages/home/home'
      })
      }else{
        Taro.atMessage({
          'message': '请输入正确账号密码',
          'type': type,
        })
      }
    })
  }
  const handleChange1=(u)=>{
    setUserName(u)
  }
  const handleChange2=(p)=>{
    setPass(p);
  }
  
  //注册
  const register = () => {
    Taro.navigateTo({
      url: '/pages/register/register'
    })
  }
    return (
      <View className='login'>
        <View className='d-f x-s-b y-c tou'>
          <View>
            <View className='hello'>您好,</View>
            <View className='my-1'>欢迎来到XXX商家管理后台~</View>
          </View>
          <View>
            <Image className='img'src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp15.qhimg.com%2Fbdr%2F740__%2Fd%2F_open360%2Fdongman1031%2F11.jpg&refer=http%3A%2F%2Fp15.qhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1631346022&t=5384595214a601f7d1b0e74030f5109c'/>
          </View>
        </View>
        <View>
          <AtForm>
            <AtInput 
              name='username '
              title='账号:' 
              type='text' 
              placeholder='请输入账号' 
              value={username}
              onChange={handleChange1.bind(this)} 
            />
            <AtInput 
              name='pass'
              value={pass}
              title='密码:' 
              type='password' 
              placeholder='请输入密码' 
              onChange={handleChange2.bind(this)} 
            /> 
            <View className='register d-f x-r my-3' onClick={register}>注册</View>
            <AtButton className='btn' onClick={home.bind(this,'error')}>登录</AtButton>
          </AtForm>
        </View>
        
        <AtMessage />
      </View>
    )
  
}

export default Login;
